<template>
  <div class="i18n-container">
    <div class="i18n-actions">
      <a href="http://localhost:3000/i18n/export?format=csv&languageTags=zh-CN,en-US&download=true&filename=i18n-data" download="download=true" target="_blank">导出</a>
      <el-button type="primary" size="small">导入</el-button>
      <el-button type="primary" size="small">新建</el-button>
    </div>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="language_tag" label="语言标签" width="180" />
      <el-table-column prop="code" label="国际化标识" width="180" />
      <el-table-column prop="message" label="文本值" />
      <el-table-column prop="remarks" label="备注" />
      <el-table-column prop="created_at" label="创建时间" />
      <el-table-column fixed="right" label="操作" min-width="120">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleEdit(scope.row)">
            编辑
          </el-button>
          <el-popconfirm
            title="确定要删除这条数据吗？"
            confirm-button-text="确认"
            cancel-button-text="取消"
            @confirm="handleDelete(scope.row)"
            @cancel="handleCancel"
          >
            <template #reference>
              <el-button link type="danger" size="small">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="currentPageSize"
      :page-sizes="[10, 20, 30, 40]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="getList"
      @current-change="getList"
    />

    <I18nEditForm
      v-model:visible="dialogVisible"
      :edit-data="editForm"
      @submit="handleFormSubmit"
      @close="handleFormClose"
    />
  </div>
</template>

<script setup>
import { getI18nList, I18nDelete, I18nUpdate, exportI18nCSV } from '@/API/i18n'
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import I18nEditForm from '@/components/I18N/compile.vue'

const tableData = ref([])
const currentPage = ref(1)
const currentPageSize = ref(10)
const total = ref(0)
const dialogVisible = ref(false)
const editForm = ref({})

const getList = async () => {
  const res = await getI18nList({
    current: currentPage.value,
    pageSize: currentPageSize.value,
  })
  if (res.code === 200) {
    tableData.value = res.data.records
    total.value = res.data.total
  } else {
    ElMessage.error(res.message || '获取数据失败')
  }
}

// 删除
const handleDelete = async (row) => {
  try {
    const res = await I18nDelete({ code: row.code })
    if (res.code === 200) {
      ElMessage.success('删除成功')
      getList()
    } else {
      ElMessage.error(res.message || '删除失败')
    }
  } catch (error) {
    ElMessage.error('请求出错，请稍后重试')
    console.error('删除请求失败:', error)
  }
}

const handleCancel = () => {
  console.log('用户取消删除')
}

const handleEdit = (row) => {
  editForm.value = { ...row }
  dialogVisible.value = true
}

const handleFormSubmit = async (formData) => {
  try {
    const requestData = {
      code: formData.code, //编码
      remarks: formData.remarks || '', //备注
      languageTexts: [
        {
          languageTag: formData.language_tag, //语言文本
          message: formData.message,
        },
      ],
    }
    const res = await I18nUpdate(requestData)
    if (res.code === 200) {
      ElMessage.success('更新成功')
      dialogVisible.value = false
      getList()
    } else {
      ElMessage.error(res.message || '更新失败')
    }
  } catch (error) {
    ElMessage.error('请求出错，请稍后重试')
    console.error('更新请求失败:', error)
  }
}

const handleFormClose = () => {
  editForm.value = {}
}



onMounted(() => {
  getList()
})
</script>

<style scoped>
.i18n-container {
  background-color: #ebeaea;
  position: relative;
  padding: 20px;
}

.i18n-actions {
  position: absolute;
  top: -50px;
  right: 20px;
  z-index: 999;
}

.i18n-actions .el-button {
  background-color: #f9f9f9;
  color: #333;
  border-color: #ccc;
}
</style>
